#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''eof
name:已结清关注信贷笔数
code:SettledLoansNum_of_SpecialMention
tableName:
columnName:
groups:企业征信
dependencies:IC_DSJ_2D
type:常用指标
dataSourceType:在线指标
description:
eof'''

'''
已结清不良、关注类贷款/五级分类
ECRPaidBadLoanInfo
'''


import sys
import pandas as pd
import datetime
import json

reload(sys)
sys.setdefaultencoding('utf-8')

#如果这个字段存在 但是里面的list长度为0 不包含任何dict 应该返回缺失值 不应该为0
def FormatData(tableName):
    try:
        ecr = json.loads(EC[0]["ecr"])[tableName]
        return ecr if isinstance(ecr, list) and len(ecr) > 0 else [{}]
    except:
        return [{}]

def SettledLoansNum_of_SpecialMention():
    null_type_list = ['', None, 'null', 'Null', 'NULL', '/',' ']
    count = []
    try:
        ECRPaidBadLoanInfo = FormatData('ECRPaidBadLoanInfo')
        if ECRPaidBadLoanInfo == [{}]:
            
            return u'缺失值'
        else:
            if len(ECRPaidBadLoanInfo) >=1:
                for i in ECRPaidBadLoanInfo:
                    if i.has_key('Fiveclasscode') and i['Fiveclasscode'] not in null_type_list:
                        if i['Fiveclasscode'] == '关注':
                            count.append(1)
                        else:
                            count.append(0)
                    else:
                        count.append(None)
                if None in set(count) and len(set(count)) == 1:
                    return u'缺失值'
                else:
                    count_notnone = [i for i in count if i!=None]
                    return sum(count_notnone)

            else:
                return u'缺失值'

    except:
        return u'缺失值'

def isfloat(x):
    try:
        if isinstance(float(x), float) and float(x)>=0:
            return True
        else:
            return False
    except:
        return False

result = SettledLoansNum_of_SpecialMention()